﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Sparklings.Gui
{
    class LevelContainer
    {
        public LevelContainer(SparklingsGame game, Level l, Rectangle boundingBox)
        {
            Game = game;
            Level = l;
            BoundingBox = boundingBox;
            LoadGraphicsRelatedContent(game.Graphics.GraphicsDevice);
        }
        public void LoadGraphicsRelatedContent(GraphicsDevice graphicsDevice)
        {
            RestartLevelTexture = Game.Content.Load<Texture2D>("level_meta_restart");
            ExitLevelTexture = Game.Content.Load<Texture2D>("level_meta_exit");
            PauseLevelTexture = Game.Content.Load<Texture2D>("level_meta_pause");
            ResumeLevelTexture = Game.Content.Load<Texture2D>("level_meta_resume");
            SpriteBatch = new SpriteBatch(graphicsDevice);
        }
        protected void ResizeLevelForUI()
        {
            Level.RenderBounds = new Vector2(BoundingBox.Width, BoundingBox.Height);
            if (Level.RenderBounds.X + UIPixelSize < BoundingBox.Width)
            {
                UIPosition = EUIPosition.Right;
            }
            else if (Level.RenderBounds.Y + UIPixelSize < BoundingBox.Height)
            {
                UIPosition = EUIPosition.Bottom;
            }
            else
            {
                // TODO: Handle the other case.
                Level.RenderBounds = new Vector2(BoundingBox.Width - UIPixelSize, BoundingBox.Height);
                UIPosition = EUIPosition.Right;
            }
        }
        public void Update(GameTime t, bool click)
        {
            if (!LevelPaused)
            {
                Level.Update(
                    t,
                    new Vector2(Mouse.GetState().X, Mouse.GetState().Y) - new Vector2(BoundingBox.Left, BoundingBox.Top),
                    Mouse.GetState().LeftButton == ButtonState.Pressed);
            }
            if (click)
            {
                if (PauseResumeButtonPosition.Contains(Mouse.GetState().X, Mouse.GetState().Y))
                {
                    LevelPaused = !LevelPaused;
                }
                else if (RestartButtonPosition.Contains(Mouse.GetState().X, Mouse.GetState().Y))
                {
                    Restart();
                }
                else if (ExitButtonPosition.Contains(Mouse.GetState().X, Mouse.GetState().Y))
                {
                    ExitLevel();
                }
            }
        }
        public Texture2D GetDrawn(GraphicsDevice graphicsDevice)
        {
            ResizeLevelForUI();
            Texture2D drawnLevel = Level.GetDrawn(graphicsDevice);
            var renderTarget = new RenderTarget2D(graphicsDevice, BoundingBox.Width, BoundingBox.Height);
            graphicsDevice.SetRenderTarget(renderTarget);
            graphicsDevice.Clear(Color.FromNonPremultiplied(63, 47, 37, 255));
            SpriteBatch.Begin();
            SpriteBatch.Draw(drawnLevel, new Vector2(), Color.White);
            if (LevelPaused)
            {
                SpriteBatch.Draw(ResumeLevelTexture, PauseResumeButtonPosition, Color.White);
            }
            else
            {
                SpriteBatch.Draw(PauseLevelTexture, PauseResumeButtonPosition, Color.White);
            }
            SpriteBatch.Draw(RestartLevelTexture, RestartButtonPosition, Color.White);
            SpriteBatch.Draw(ExitLevelTexture, ExitButtonPosition, Color.White);
            SpriteBatch.End();
            graphicsDevice.SetRenderTarget(null);
            return renderTarget;
        }
        public void Restart()
        {
            Game.StartLevel(Game.CurrentLevelMetaData);
        }
        public void ExitLevel()
        {
            Game.ExitLevel();
        }
        private Rectangle PauseResumeButtonPosition
        {
            get
            {
                if (UIPosition == EUIPosition.Bottom)
                {
                    return new Rectangle(BoundingBox.Left, BoundingBox.Bottom - UIPixelSize, UIPixelSize, UIPixelSize);
                }
                else
                {
                    return new Rectangle(BoundingBox.Right - UIPixelSize, BoundingBox.Top, UIPixelSize, UIPixelSize);
                }
            }
        }
        private Rectangle RestartButtonPosition
        {
            get
            {
                if (UIPosition == EUIPosition.Bottom)
                {
                    return new Rectangle(BoundingBox.Left + UIPixelSpacing, BoundingBox.Bottom - UIPixelSize, UIPixelSize, UIPixelSize);
                }
                else
                {
                    return new Rectangle(BoundingBox.Right - UIPixelSize, BoundingBox.Top +  UIPixelSpacing, UIPixelSize, UIPixelSize);
                }
            }
        }
        private Rectangle ExitButtonPosition
        {
            get
            {
                if (UIPosition == EUIPosition.Bottom)
                {
                    return new Rectangle(BoundingBox.Left + UIPixelSpacing * 2, BoundingBox.Bottom - UIPixelSize, UIPixelSize, UIPixelSize);
                }
                else
                {
                    return new Rectangle(BoundingBox.Right - UIPixelSize, BoundingBox.Top + UIPixelSpacing * 2, UIPixelSize, UIPixelSize);
                }
            }
        }
        private SparklingsGame Game;
        public Level Level
        {
            get { return _level; }
            set { _level = value; LevelPaused = false; }
        }
        private Level _level;
        private Texture2D RestartLevelTexture;
        private Texture2D ExitLevelTexture;
        private Texture2D PauseLevelTexture;
        private Texture2D ResumeLevelTexture;
        private Rectangle BoundingBox;
        private SpriteBatch SpriteBatch;
        private enum EUIPosition
        {
            Bottom,
            Right
        }
        public bool LevelPaused;
        private EUIPosition UIPosition;
        private readonly int UIPixelSize = 32;
        private readonly int UIPixelSpacing = 48;
    }
}
